package com.baizhi.service.impl;

import com.baizhi.dao.CategoryDao;
import com.baizhi.dto.CategoryDTO;
import com.baizhi.entity.Category;
import com.baizhi.po.AddCategoryPO;
import com.baizhi.po.CategoryPO;
import com.baizhi.po.CategorysPO;
import com.baizhi.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 分类(Category)表服务实现类
 *
 * @author makejava
 * @since 2021-09-23 20:13:56
 */
@Service("categoryService")
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Resource
    private CategoryDao categoryDao;


    @Override
    public List<CategoryPO> queryListByParentId() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(Category::getParentId);
        queryWrapper.isNull(Category::getDeletedAt);
        List<Category> categories = categoryDao.selectList(queryWrapper);
        List<CategoryPO> categoryPOS = new ArrayList<>();

        for (Category category : categories) {
            CategoryPO e = new CategoryPO();
            e.setId(category.getId());
            e.setName(category.getName());
            e.setParentId(category.getParentId());
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
            wrapper.isNull(Category::getDeletedAt);
            wrapper.eq(Category::getParentId, category.getId());
            List<Category> categories1 = categoryDao.selectList(wrapper);
            List<CategorysPO> categorysPOS = new ArrayList<>();
            e.setChildren(categorysPOS);
            for (Category category1 : categories1) {
                CategorysPO categorysPO = new CategorysPO();
                BeanUtils.copyProperties(category1, categorysPO);
                categorysPOS.add(categorysPO);
            }
            categoryPOS.add(e);
        }
        //说明是一级标签
        //根据一级标签查取对应的二级标签

        return categoryPOS;
    }

    @Override
    public void DeleteById(Integer id) {
        System.out.println(id);
        Category category = categoryDao.selectById(id);
        System.out.println("category = " + category);
        //如果是一级标签查询是否有二级标签没有才可以删除
        if (category.getParentId() != null) {
            //有则是二级标签
            Category category1 = categoryDao.selectById(id);
            category1.setDeletedAt(new Date());
            categoryDao.updateById(category1);
        } else {
            //没有就是一级标签
            //根据一级标签，查是否有二级标签
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Category::getParentId, category.getId());
            List<Category> categories = categoryDao.selectList(queryWrapper);
            if (categories != null) {
                // TODO 有二级标签不能删除

            } else {
                Category category1 = categoryDao.selectById(id);
                category1.setDeletedAt(new Date());
                categoryDao.updateById(category1);
            }
        }

    }

    @Override
    public AddCategoryPO add(CategoryDTO categoryDTO) {
        Category entity = new Category();
        BeanUtils.copyProperties(categoryDTO, entity);
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        int insert = categoryDao.insert(entity);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId, entity.getId());
        Category category = categoryDao.selectOne(queryWrapper);
        AddCategoryPO addCategoryPO = new AddCategoryPO();
        BeanUtils.copyProperties(category, addCategoryPO);
        return addCategoryPO;
    }

    @Override
    public AddCategoryPO update(Integer id, CategoryDTO categoryDTO) {
        Category entity = new Category();
        BeanUtils.copyProperties(categoryDTO, entity);
        entity.setId(id);
        entity.setUpdatedAt(new Date());
        int update = categoryDao.updateById(entity);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId, entity.getId());
        Category category = categoryDao.selectOne(queryWrapper);
        AddCategoryPO addCategoryPO = new AddCategoryPO();
        BeanUtils.copyProperties(category, addCategoryPO);
        return addCategoryPO;
    }

    @Override
    public String queryById(Integer id) {
        Category category = categoryDao.selectById(id);
        if (category.getDeletedAt() != null) {
            return category.getName();
        } else {
            return "没有该标签";
        }
    }
}
